#include <fstream>
#include <vector>

using namespace std;

__int64 OneRound(__int64 k, vector<__int64>& que, __int64& pos)
{
	__int64 start = pos;
	__int64 money =0;

	while ( money ==0 || (k >= que[pos] && pos!=start) )
	{
		k-= que[pos];
		money += que[pos];
		pos = (pos +1) % que.size();
	}

	return money;
}

__int64 CalcMoney (__int64 R, __int64 k, vector<__int64>& que)
{
	vector<__int64>	round(que.size());
	vector<__int64>	money(que.size());
	vector<__int64>	total;

	__int64 totalMoney = 0;
	__int64 pos =0;

	for (__int64 r=0; r<R; r++)
	{
		total.push_back(totalMoney);
		
		if (money[pos] ==0 )
		{
			round[pos] =r;
			__int64 lastPos = pos;
			__int64 m = OneRound(k, que, pos);
			money[lastPos] = m;
			totalMoney += m;
		}	
		else
		{
			__int64 roundStart = round[pos];
			__int64 size = r - roundStart;
			__int64 loop = (R - roundStart) / size;

			__int64 headMny = total[roundStart];
			__int64 loopMny = totalMoney -headMny;
			__int64 tail = (R - roundStart) % size;
			__int64 tailMny = total[tail + roundStart] - total[roundStart];

			totalMoney =  headMny +loopMny * loop +tailMny;
			break;
		}
	}

	return totalMoney;
}

void RunC(const char* fnameIn, const char* fnameOut)
{
	ifstream fin(fnameIn);
	
	__int64 caseCount;
	fin >> caseCount;

	ofstream fout(fnameOut);
	__int64 R, k, groups;

	for (__int64 i=0; i<caseCount; i++)
	{
		fin >> R >> k >> groups;

		vector<__int64> que;
		for (__int64 j=0; j<groups; j++)
		{
			__int64 g;
			fin>>g;
			que.push_back(g);
		}
		
		fout<<"Case #"<<i+1<<": ";
		fout<< CalcMoney (R, k, que);
		fout<<"\n";
	}
}
